<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Example
 *
 * This is an example of a few basic user interaction methods you could use
 * all done with a hardcoded array.
 *
 * @package     CodeIgniter
 * @subpackage  Rest Server
 * @category    Controller
 * @author      lcdung
 * @link        http://bungtay.com/hrm3/
*/

// This can be removed if you use __autoload() in config.php OR use Modular Extensions
require APPPATH.'/libraries/REST_Controller.php';

class Report extends REST_Controller{

    function __construct(){

        parent::__construct();

        //$this->load->model('user','',TRUE);

        $this->load->model(array('report_model','report_model','staff_model','department_model','user_model'));

    }

    
    public function paylist_post(){
        $params = json_decode(file_get_contents('php://input'), TRUE);

            $token = $this->get('token');

            $this->oauth($token);

        $data           =   array();
        $salary         =   $this->report_model->getpaygrade(2);
        $staffid          =   $params['staff'];
        $staff = $this->staff_model->getSingleStaff(array('idstaff'=>$staffid));
        
        $data['datestart']  =   $datestart      =   $params['datestart'];
        $data['dateend']    =   $dateend        =   $params['dateend'];
        $data['salary']     =   $salary['salary'];
        $data['staffcode']  =   $staff['codestaff'];
        $data['staff']      =   $staff['firstname'].' '.$staff['lastname'];
        
        //$datestart      = str_replace('/','-',$datestart) ;
        // $datestart      = strtotime($datestart);
        
        
        //$dateend        =   str_replace('/','-',$dateend) ;
        // $dateend        = strtotime($dateend); 
        
        $param      =   array(
                            'datestart'=>$datestart,
                            'dateend'=>$dateend,
                            'idstaff'=>$staffid,
                        );
        $data['timesheet'] = $this->report_model->gettimesheetnstaff($param);

        if(!empty($data)){
                $this->response(array('result'=>$data), 200);    
            }else{
                $this->response(array('result'=>'false'), 200);    
            }
        //print_r($param);
        //print_r($data);
        
        // $this->load->view('report/print_paylist',$data);
    }
    
    public function summary_post(){

        $params = json_decode(file_get_contents('php://input'), TRUE);

            $token = $this->get('token');

            $this->oauth($token);

        $data           =   array();
        
        $salary         =   $this->report_model->getpaygrade(2);
        
        $data['datestart']  =   $datestart      =   $params['datestart'];
        $data['dateend']    =   $dateend        =   $params['dateend'];
        $data['salary']     =   $salary['salary'];
        $data['countstaff'] = $this->staff_model->countall();
        $data['countdepartment'] = $this->department_model->countall();
        
        //$datestart      = str_replace('/','-',$datestart) ;
        // $datestart      = strtotime($datestart);
        
        //$dateend        =   str_replace('/','-',$dateend) ;
        // $dateend        = strtotime($dateend); 
        
        $param      =   array(
                            'datestart'=>$datestart,
                            'dateend'=>$dateend,
                        );
        $data['summary'] = $this->report_model->getsummary($param);

        if(!empty($data)){
                $this->response(array('result'=>$data), 200);    
            }else{
                $this->response(array('result'=>'false'), 200);    
            }
        //print_r($param);
        //echo '<pre>';
//        print_r($data);
//        echo '</pre>';
        
        // $this->load->view('report/print_summary',$data);
    }

    function oauth($token,$token_secret=null){
        //token=ab3cd9j4ks73hf7g&token_secret=xyz4992k83j47x0b
        $oauth = $this->user_model->auth_token($token);
        if($oauth){
            if(time()<$oauth['date_created']){
                    $cnttime = time()+10800;
                        
                    $data = array('date_created'=>$cnttime);
                    $cont = array('id' => $oauth['id']);
                    $xs = $this->user_model->updateKey($data,$cont);
                    //$this->response($xs, 200);
            }else{
                $this->response(array('error' => 'Wrong token'), 401);
            }
        }else{
            $this->response(array('error' => 'Wrong token'), 401);
        }
    }

}



/* End of file welcome.php */

/* Location: ./application/controllers/welcome.php */